package com.tanhua.server.controller;


import com.tanhua.commons.utils.UserHolder;
import com.tanhua.model.domain.db.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.service.BlacklistService;
import com.tanhua.server.service.QuestionService;
import com.tanhua.server.service.UserService;
import com.tanhua.server.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
@RequestMapping("users")
public class UsersController {

    @Autowired
    private UsersService usersService;
    @Autowired
    private UserService userService;
    @Autowired
    private QuestionService questionService;
    @Autowired
    private BlacklistService blacklistService;

    /**
     * 1、获取当前登陆者个人信息资料
     * @return
     */
    @GetMapping
    public ResponseEntity<UserInfoVo> findUserInfoById(){
        Long id = UserHolder.getUserId();
        UserInfoVo userInfoVo=usersService.findUserInfoById(id);
        return ResponseEntity.ok(userInfoVo);
    }


    /**
     * 2、用户资料编辑

     * @param userInfo
     * @return
     */
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo){
        usersService.updateUserInfo(userInfo);
        return ResponseEntity.ok(null);
    }

    //修改头像
    @PostMapping("header")
    public ResponseEntity updateHead( MultipartFile headPhoto){
        userService.uploadHead(headPhoto);
        return ResponseEntity.ok(null);
    }

    //获取通用配置
    @GetMapping("settings")
    public ResponseEntity settings(){
        Long id = UserHolder.getUserId();
        SettingsVo setting =usersService.findSetting(id);
        return ResponseEntity.ok(setting);
    }

    //修改陌生人问题
    @PostMapping("questions")
    public ResponseEntity setQuestions(@RequestBody Map content){
        Long id = UserHolder.getUserId();
        questionService.setQuestions((String) content.get("content"));
        return ResponseEntity.ok(null);
    }

    //获取黑名单
    @GetMapping("blacklist")
    public ResponseEntity blacklist(@RequestParam(required = false,defaultValue = "1") Integer page,
                                    @RequestParam(required = false,defaultValue = "3")Integer pagesize){
        Long id = UserHolder.getUserId();
        PageResult pageResult = blacklistService.blacklist(id,page,pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 取消黑名单
     */
    @DeleteMapping("/blacklist/{uid}")
    public ResponseEntity deleteBlackList(@PathVariable("uid") Long blackUserId) {
        blacklistService.deleteBlackList(blackUserId);
        return ResponseEntity.ok(null);
    }


}
